package com.lanhai.zhonglai.impl;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.google.gson.Gson;
import com.lanhai.zhonglai.beans.StationMountBean;
import com.lanhai.zhonglai.interfaces.IStationMount;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

@Service
public class StationMountImpl implements IStationMount {
    @Autowired
    JedisPool jedisPool;

    static Map<Integer, StationMountBean> mountMap = new ConcurrentHashMap<Integer, StationMountBean>();


    /**
     * 获取redis中的数据
     */
    @Override
    public StationMountBean getBean(int dotno) {

        StationMountBean bean = mountMap.get(dotno);
        if (bean == null) {
            String mainkey = "stationMount";
            String key = String.format("%d", dotno);
            Gson gson = new Gson();

            Jedis jd = jedisPool.getResource();
            String json = jd.hget(mainkey, key);
            jd.close();
            if (json != null && !json.isEmpty()) {
                try {
                    bean = gson.fromJson(json, StationMountBean.class);
                } catch (Exception e) {
                }
            }
            if (bean != null) {
                mountMap.put(dotno, bean);
            } else {
                bean = new StationMountBean();
                bean.setDotno(dotno);
                bean.setAllowIn(false);
                bean.setAllowOut(false);
                bean.setInNum(0);
                bean.setOutNum(0);
                mountMap.put(dotno, bean);

            }
        }
        return bean;
    }

    /**
     * 保存数据
     */
    @Override
    public void saveCache(int dotno, StationMountBean bean) {
        if (bean == null) return;
        bean.setDotno(dotno);
        mountMap.put(dotno, bean);

        saveToRedis(dotno, bean);
    }

    /**
     * 从redis中移除
     */
    @Override
    public void clear(int dotno) {

        StationMountBean bean = mountMap.get(dotno);
        if (bean == null) {
            bean = new StationMountBean();
            bean.setDotno(dotno);
        }
        bean.setAllowIn(false);
        bean.setAllowOut(false);
        bean.setInNum(1);
        bean.setOutNum(0);
        mountMap.put(dotno, bean);

        saveToRedis(dotno, bean);
    }

    /**
     * 手动将机台数量填满，达到传输要求
     */
    @Override
    public void fullStationMount(int dotno, int transnum) {
        StationMountBean bean = mountMap.get(dotno);
        if (bean == null) {
            bean = new StationMountBean();
            bean.setDotno(dotno);
        }
        bean.setAllowIn(true);
        bean.setAllowOut(true);
        bean.setInNum(0);
        bean.setOutNum(transnum);
        mountMap.put(dotno, bean);

        saveToRedis(dotno, bean);
    }


    void saveToRedis(int dotno, StationMountBean bean) {
        String mainkey = "stationMount";
        String key = String.format("%d", dotno);
        Gson gson = new Gson();
        String json = gson.toJson(bean);
        Jedis jd = jedisPool.getResource();
        jd.hset(mainkey, key, json);
        jd.close();
    }

}
